﻿Imports System.Text
Imports System.Threading
Imports System.IO
Imports NLog

Public Class LogHelper

    Public Sub GenerateCreatorLog(ByVal form As System.Windows.Forms.Form, ByVal richtextbox As System.Windows.Forms.RichTextBox)

        Try

            Dim _debug_target As New NLog.Targets.DebuggerTarget
            Dim _debug_log_rule As New NLog.Config.LoggingRule
            Dim _log_config As New NLog.Config.LoggingConfiguration
            Dim _default_log_rule As New NLog.Config.LoggingRule
            Dim _gui_target As New NLog.Windows.Forms.RichTextBoxTarget

            _log_config.AddTarget("Debug", _debug_target)
            _debug_log_rule = New NLog.Config.LoggingRule("*", NLog.LogLevel.Debug, _debug_target)

            _gui_target.AutoScroll = True
            _gui_target.UseDefaultRowColoringRules = True
            _gui_target.TargetForm = form
            _gui_target.TargetRichTextBox = richtextbox

            _log_config.AddTarget("gui", _gui_target)
            _default_log_rule.Targets.Add(_gui_target)

            _log_config.LoggingRules.Add(_default_log_rule)
            _log_config.LoggingRules.Add(_debug_log_rule)

            NLog.LogManager.Configuration = _log_config

        Catch ex As Exception

        End Try


    End Sub

    Public Sub GenerateNLogConfig(Optional ByVal FileName As String = "")

        Dim _log_config As New NLog.Config.LoggingConfiguration
        Dim _default_log_rule As New NLog.Config.LoggingRule
        Dim _console_target As New NLog.Targets.ColoredConsoleTarget

        _console_target.UseDefaultRowHighlightingRules = True
        _log_config.AddTarget("Console", _console_target)

        _default_log_rule = New NLog.Config.LoggingRule("*", NLog.LogLevel.Info, _console_target)


#Region "Debug Log"

#If DEBUG Then

        Dim _debug_log_rule As New NLog.Config.LoggingRule
        Dim _debug_target As New NLog.Targets.DebuggerTarget

        _log_config.AddTarget("Debug", _debug_target)

        _debug_log_rule = New NLog.Config.LoggingRule("*", NLog.LogLevel.Debug, _debug_target)
        _log_config.LoggingRules.Add(_debug_log_rule)

#End If


#End Region



#Region "FileLog"

        If Not String.IsNullOrEmpty(FileName) Then

            Dim _filelog_target As New NLog.Targets.FileTarget

            With _filelog_target

                .AutoFlush = True
                .FileName = "${basedir}/" & FileName
                .CreateDirs = True
                .ConcurrentWrites = True
                .KeepFileOpen = True
                .ArchiveOldFileOnStartup = True

            End With

            _log_config.AddTarget("file", _filelog_target)
            _default_log_rule.Targets.Add(_filelog_target)

        End If

#End Region


        _log_config.LoggingRules.Add(_default_log_rule)
        NLog.LogManager.Configuration = _log_config


    End Sub

    Public Sub AddGUILogTarget()

        Dim _gui_target As New NLog.Windows.Forms.FormControlTarget
        Dim _log_form As New frm_Log
        Dim _log_config As NLog.Config.LoggingConfiguration

        _log_config = NLog.LogManager.Configuration

        _log_form.Show()

        '_gui_target.AutoScroll = True
        '_gui_target.MaxLines = 0
        '_gui_target.UseDefaultRowColoringRules = True
        _gui_target.FormName = "frm_Log"
        _gui_target.ControlName = "rtb_log"
        '_gui_target.ToolWindow = False
        '_gui_target.ShowMinimized = False
        '_gui_target.Width = 500
        '_gui_target.Height = 370

        _log_config.AddTarget("gui", _gui_target)

        For Each _logrule In _log_config.LoggingRules

            If Not _logrule.IsLoggingEnabledForLevel(NLog.LogLevel.Debug) Then
                _logrule.Targets.Add(_gui_target)
            End If

        Next

        NLog.LogManager.Configuration = _log_config

    End Sub

End Class
